/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_NET_HTTP_HTTP_SERVER_H_
#define NODE_NET_HTTP_HTTP_SERVER_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <event2/event.h>
#include <event2/http.h>
#include "node/macros.h"

namespace node {
    namespace http {
        class HttpRequest;
        class HttpResponse;

        typedef void(*HTTP_REQUEST_CALLBACK)(::node::http::HttpRequest *, ::node::http::HttpResponse *, void *);

        class HttpServer
        {
        public:
            HttpServer(const char *doc_root);
            ~HttpServer(void);

            bool Start(const char *host, int port);
            void Stop(void);
            void Run(void);

            inline void set_user_data(void *user_data) {
                user_data_ = user_data;
            }
            inline void *get_user_data(void) const {
                return user_data_;
            }
            inline void set_request_callback(HTTP_REQUEST_CALLBACK callback) {
                request_callback_ = callback;
            }

        private:
            static void OnRequest(struct evhttp_request *request, void *arg);
            static void OnTimeout(evutil_socket_t, short, void *);

            const char *doc_root_;
            struct event_base *ebase_;
            struct event *edummy_;
            struct evhttp *http_;
            struct evhttp_bound_socket * bound_handle_;
            HTTP_REQUEST_CALLBACK request_callback_;
            void *user_data_;
            DISALLOW_COPY_AND_ASSIGN(HttpServer);
        };

    }
}

#endif // NODE_NET_HTTP_HTTP_SERVER_H_
